//undetermined :1、首次授权，未确定是否可使用此权限，此时调用对应的API时系统会弹出提示框让用户确认  
//authorized:	之前同意了授权
//denied:	之前拒绝了权限
//none:	无此权限
/// null = 未请求，1 = 已允许，0 = 拒绝|受限, 2 = 系统未开启
const IosPermission = {
	album(resolve, reject) {
		var result = 'denied';
		var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
		var authStatus = PHPhotoLibrary.authorizationStatus();
		if (authStatus === 0) {
			result = 'undetermined';
			resolve(result);
		} else if (authStatus == 3) {
			result = 'authorized';
			resolve(result);
		} else {
			result = 'denied';
			reject(result,{code:-1,message:'权限没有开启'});
		}
		plus.ios.deleteObject(PHPhotoLibrary);
		return result;
	},

	camera(resolve, reject) {
		var result = 'denied';
		var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
		var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
		if (authStatus === 0) {
			result = 'undetermined';
			resolve(result);
		} else if (authStatus == 3) {
			result = 'authorized';
			resolve(result);
		} else {
			result = 'denied';
			reject(result,{code:-1,message:'权限没有开启'});
		}
		plus.ios.deleteObject(AVCaptureDevice);
		return result;
	},
	location(resolve, reject) {
		var result = 'denied';
		var cllocationManger = plus.ios.import("CLLocationManager");
		var enable = cllocationManger.locationServicesEnabled();
		var status = cllocationManger.authorizationStatus();
		if (!enable) {
			result = 'error';
			reject(result,{code:-1,message:'系统定位未开启'});
		} else if (status === 0) {
			result = 'undetermined';
			resolve(result);
		} else if (status === 3 || status === 4) {
			result = 'authorized';
			resolve(result);
		} else {
			result = 'denied';
			reject(result,{code:-1,message:'权限没有开启'});
		}
		plus.ios.deleteObject(cllocationManger);
		return result;
	},

	push(resolve, reject) {
		var result = 'denied';
		var UIApplication = plus.ios.import("UIApplication");
		var app = UIApplication.sharedApplication();
		var enabledTypes = 0;
		if (app.currentUserNotificationSettings) {
			var settings = app.currentUserNotificationSettings();
			enabledTypes = settings.plusGetAttribute("types");
			if (enabledTypes == 0) {
				result = 'denied';
				reject(result,{code:-1,message:'权限没有开启'});
				console.log("推送权限没有开启");
			} else {
				result = 'authorized';
				resolve(result);
				console.log("已经开启推送功能!")
			}
			plus.ios.deleteObject(settings);
		} else {
			enabledTypes = app.enabledRemoteNotificationTypes();
			if (enabledTypes == 0) {
				result = 'denied'//3;
				reject(result,{code:-1,message:'权限没有开启'});
				console.log("推送权限没有开启!");
			} else {
				result = 'authorized'//4;
				resolve(result);
				console.log("已经开启推送功能!")
			}
		}
		plus.ios.deleteObject(app);
		plus.ios.deleteObject(UIApplication);
		return result;
	},

	contact(resolve, reject) {
		var result = 'denied';
		var CNContactStore = plus.ios.import("CNContactStore");
		var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
		if (cnAuthStatus === 0) {
			result = 'undetermined';
			resolve(result);
		} else if (cnAuthStatus == 3) {
			result = 'authorized';
			resolve(result);
		} else {
			result = 'denied';
			reject(result,{code:-1,message:'权限没有开启'});
		}
		plus.ios.deleteObject(CNContactStore);
		return result;
	},

	record(resolve, reject) {
		var result = 'undetermined';
		var avaudiosession = plus.ios.import("AVAudioSession");
		var avaudio = avaudiosession.sharedInstance();
		var status = avaudio.recordPermission();
		if (status === 1970168948) {
			result = 'undetermined';
			resolve(result);
		} else if (status === 1735552628) {
			result = 'authorized';
			resolve(result);
		} else {
			result = 'denied';
			reject(result,{code:-1,message:'权限没有开启'});
		}
		plus.ios.deleteObject(avaudiosession);
		return result;
	},

	calendar(resolve, reject) {
		var result = 'undetermined';
		var EKEventStore = plus.ios.import("EKEventStore");
		var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
		if (ekAuthStatus == 3) {
			result = 'authorized';
			resolve(result);
			console.log("日历权限已经开启");
		} else {
			result = 'denied';
			reject(result,{code:-1,message:'权限没有开启'});
			console.log("日历权限没有开启");
		}
		plus.ios.deleteObject(EKEventStore);
		return result;
	},

	memo(resolve, reject) {
		var result = 'undetermined';
		var EKEventStore = plus.ios.import("EKEventStore");
		var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
		if (ekAuthStatus == 3) {
			result = 'authorized';
			resolve(result);
			console.log("备忘录权限已经开启");
		} else {
			result = 'denied';
			reject(result,{code:-1,message:'权限没有开启'});			
			console.log("备忘录权限没有开启");
		}
		plus.ios.deleteObject(EKEventStore);
		return result;
	},


	check(permissionID) {
		var permissionIdentity = permissionID.toUpperCase();
		return new Promise((resolve, reject) => {
			switch (permissionIdentity) {
				case "PUSH":
					this.push(resolve, reject);
					break;
				case "LOCATION":
					this.location(resolve, reject);
					break;
				case "RECORD":
					this.record(resolve, reject);
					break;
				case "CAMERA":
					this.camera(resolve, reject);
					break;
				case "ALBUM":
					this.album(resolve, reject);
					break;
				case "CONTACT":
					this.contact(resolve, reject);
					break;
				case "CALENDAR":
					this.calendar(resolve, reject);
					break;
				case "MEMO":
					this.memo(resolve, reject);
					break;
				default:
					reject('none',{code: -1,message: '无须授权'});
					break;
			}
		});
	},
	request(permissionID){
		return this.check(permissionID);
	},
	setting(permissionID='') {
		
		var UIApplication = plus.ios.import("UIApplication");
        var application2 = UIApplication.sharedApplication();
        var NSURL2 = plus.ios.import("NSURL");
        var setting2 = NSURL2.URLWithString("app-settings:");
        application2.openURL(setting2);
        plus.ios.deleteObject(setting2);
        plus.ios.deleteObject(NSURL2);
        plus.ios.deleteObject(application2);
	}
}
export default IosPermission;